/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.ShopRenovation;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.live.comment.model.LiveRoom;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopRenovationService;
import lombok.AllArgsConstructor;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.ObjectStreamClass;
import java.util.Objects;


/**
 * 店铺装修信息
 *
 * @author lhd
 * @date 2021-01-05 11:03:38
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopRenovation" )
public class ShopRenovationController {

    private final ShopRenovationService shopRenovationService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param shopRenovation 店铺装修信息
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<ShopRenovation>> getShopRenovationPage(PageParam<ShopRenovation> page, ShopRenovation shopRenovation) {
        return ResponseEntity.ok(shopRenovationService.page(page, new LambdaQueryWrapper<ShopRenovation>()
                .eq(ShopRenovation::getShopId,SecurityUtils.getShopUser().getShopId())));
    }


    /**
     * 通过id查询店铺装修信息
     * @param renovationId id
     * @return 单个数据
     */
    @GetMapping("/info/{renovationId}" )
    public ResponseEntity<ShopRenovation> getById(@PathVariable("renovationId") Long renovationId) {
        ShopRenovation shopRenovation = shopRenovationService.getById(renovationId);
        if(!Objects.equals(shopRenovation.getShopId() ,SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }
        return ResponseEntity.ok(shopRenovation);
    }

    /**
     * 修改主页标识
     * @param id id
     */
    @PutMapping("/updateHomePage/{id}" )
    @PreAuthorize("@pms.hasPermission('shop:shopRenovation:updateHome')")
    public ResponseEntity<Void> updateHomePage(@PathVariable Long id) {
        ShopRenovation shopRenovation = shopRenovationService.getById(id);
        if(!Objects.equals(shopRenovation.getShopId() ,SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }

        shopRenovationService.updateToHomePage(id,shopRenovation.getHomeStatus(),SecurityUtils.getShopUser().getShopId());
        return ResponseEntity.ok().build();
    }

    /**
     * 新增店铺装修信息
     * @param shopRenovation 店铺装修信息
     * @return 是否新增成功
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('shop:shopRenovation:save')")
    public ResponseEntity<Long> save(@RequestBody @Valid ShopRenovation shopRenovation) {
        shopRenovation.setShopId(SecurityUtils.getShopUser().getShopId());
        shopRenovationService.save(shopRenovation);
        return ResponseEntity.ok(shopRenovation.getRenovationId());
    }

    /**
     * 修改店铺装修信息
     * @param shopRenovation 店铺装修信息
     * @return 是否修改成功
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('shop:shopRenovation:update')")
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid ShopRenovation shopRenovation) {
        ShopRenovation shopRenovationDb = shopRenovationService.getById(shopRenovation.getRenovationId());
        if (Objects.isNull(shopRenovationDb)) {
            // 查找不到该装修信息
            throw new YamiShopBindException("yami.renovation.not.exist");
        }
        if(!Objects.equals(shopRenovationDb.getShopId() ,SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }
        return ResponseEntity.ok(shopRenovationService.updateById(shopRenovation));
    }

    /**
     * 通过id删除店铺装修信息
     * @param renovationId id
     * @return 是否删除成功
     */
    @SysLog("删除店铺装修信息" )
    @DeleteMapping("/{renovationId}" )
    @PreAuthorize("@pms.hasPermission('shop:shopRenovation:delete')")
    public ResponseEntity<Boolean> removeById(@PathVariable Long renovationId) {
        ShopRenovation shopRenovation = shopRenovationService.getById(renovationId);
        if(!Objects.equals(shopRenovation.getShopId() ,SecurityUtils.getShopUser().getShopId())){
            // 没有权限进行操作
            throw new YamiShopBindException("yami.no.auth");
        }
        return ResponseEntity.ok(shopRenovationService.removeById(renovationId));
    }

}
